package com.whh.permission.controller;

import com.github.pagehelper.PageInfo;
import com.whh.permission.po.User;
import com.whh.permission.service.IUserService;
import com.whh.permission.util.JsonObject;
import com.whh.permission.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Controller
public class UserController {
    @Resource
    private IUserService userServiceImpl;


    /*@RequestMapping("user/userAll")
    @ResponseBody
    public R queryUserInfo(Model model, User user, @RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer limit){
        PageInfo<User> userAll = userServiceImpl.findUserAll(page, limit, user);
        R ok = new R(0, "ok", userAll.getList());
        return ok;
    }*/

    @RequestMapping("user/userAll")
    @ResponseBody
    public JsonObject queryUserInfo(Model model, User user,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer limit){
        JsonObject object=new JsonObject();
        PageInfo<User> pageInfo=userServiceImpl.findUserAll(page,limit,user);
        object.setCode(0);
        object.setCount(pageInfo.getTotal());
        object.setData(pageInfo.getList());
        object.setMsg("ok");
        return object;
    }

    @RequestMapping("user/addUserSubmit")
    @ResponseBody
    public R addUserSubmit(@RequestBody User user){
        System.out.println(user);
        userServiceImpl.addUser(user);
        return R.ok();
    }


    /**
     * 删除功能
     * @return
     */
    @RequestMapping("user/deleteUserByIds")
    @ResponseBody
    public R deleteUserByIds(String ids){
        List list = Arrays.asList(ids.split(","));
        userServiceImpl.deleteUserInfoByIds(list);
        return R.ok();
    }

    /**
     * 修改功能
     * @return
     */
    @ResponseBody
    @RequestMapping("user/updateUserSubmit")
    public R updateUserSubmit(@RequestBody User user){
        userServiceImpl.updateUserSubmit(user);
        return R.ok();
    }

    @RequestMapping("updateUser")
    public String updateUser(Integer id,Model model){
        //根据用户id查询用户信息
        User user= userServiceImpl.queryUserById(id);
        model.addAttribute("user",user);
        System.out.println(user);
        return "pages/updateUser";
    }

    @RequestMapping("/addUser")
    public String addUser(){
        return "pages/addUser";
    }

    @RequestMapping("user")
    public String userIndex(){
        return "pages/user";
    }
}
